// index.js
// 获取应用实例
const app = getApp();
var http = require('../../utils/request');

Page({
  data: {
    userinfo: {},
    wechat_userinfo: {},
    shopinfo: {}
  },
  onLoad() {
    let that=this;
    that.setData({userinfo: wx.getStorageSync('userinfo')});
    that.setData({wechat_userinfo: wx.getStorageSync('wechat_userinfo')});
    that.getShopInfo();
  },

  getShopInfo() {
    let that = this;
    http.postData('/xiandu/app/v1/get_shop_bytoken', {}, (rep) => {
      if (rep.code == 200200) {
        that.setData({shopinfo: rep.data.shop_info});
      }
    })
  },

  goScan() {
    if (wx.getStorageSync('userinfo') && wx.getStorageSync('userinfo').token) {
      wx.scanCode({
        onlyFromCamera: true, // 仅仅相机
        success: (res) => {
          wx.navigateTo({
              url: `/pages/shopDetail/shopdetail?scancode=${
              res.result
            }`
          })
        },
        // 错误返回
        fail: (res) => {
          wx.showToast({title: '扫描失败，请重试~', icon: 'none', duration: 3000})
        }
      })
    } else {
      wx.reLaunch({url: '/pages/authorize/authorize'})
    }

  },
  goInsert() {
    if (wx.getStorageSync('userinfo') && wx.getStorageSync('userinfo').token) {
      wx.navigateTo({url: '/pages/handInsert/handinsert'})
    } else {
      wx.reLaunch({url: '/pages/authorize/authorize'})
    }
  },
  // 预警查询
  goShops() {
    if (wx.getStorageSync('userinfo') && wx.getStorageSync('userinfo').token) {
      wx.navigateTo({url: '/pages/shops/shoplist'})
    } else {
      wx.reLaunch({url: '/pages/authorize/authorize'})
    }
  },
  logOut(){
    http.postData('/xiandu/app/v1/login_out', {
    }, (rep) => {
    });
    wx.clearStorage();
    wx.reLaunch({url: '/pages/authorize/authorize'})
  }

})
